﻿using DI;
using Core.IPresentation;
using Library.Logging;
using Library.Structuremap;
using Microsoft.Practices.ServiceLocation;
using Presentation;
using StructureMap;
using StructureMap.Pipeline;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;

namespace Front
{
    public class MvcApplication : System.Web.HttpApplication
    {
        protected void Application_Start()
        {
            Bootstrapper.Init(new TransientLifecycle());

            AreaRegistration.RegisterAllAreas();
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);            
            GlobalConfiguration.Configure(WebApiConfig.Register);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);

            var structureMapResolver = new StructureMapDependencyResolver(Bootstrapper.Container);
            //Register for MVC
            DependencyResolver.SetResolver(structureMapResolver);
            ////Register for Web API
            GlobalConfiguration.Configuration.DependencyResolver = structureMapResolver;            
        }

        public void Application_BeginRequest()
        {         
        }

        protected void Application_EndRequest(object sender, EventArgs e)
        {         
            //HttpContextLifecycle.DisposeAndClearAll();
        }

        protected void Application_Error()
        {
            try
            {
                Exception exception = Server.GetLastError();
                ILogger logger = Bootstrapper.Resolve<ILogger>();                
                logger.Log(exception);
            }
            catch (Exception) { }
        }
    }
}
